<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SearchUI.Models.SearchModel>" %>

<% if (!ViewData.Model.SearchHasErrors)
   {
       if (ViewData.Model.ResultsArray.Length == 0)
       {
            %>

<div style="background: #eee; padding:5px;border-top: solid 1px black;"><span style="color:Black">Search of "<%= ViewData.Model.SearchTitle%>" for "<%= ViewData.Model.SearchTerms%>" returned <%= ViewData.Model.ResultsArray.Length%> results</div>
<%
    }
       else
       {
       %>
<div style="background: #eee; padding:5px;border-top: solid 1px black;"><span style="color:Black">Search of "<%= ViewData.Model.SearchTitle%>" for "<%= ViewData.Model.SearchTerms%>" returned <%= ViewData.Model.ResultsArray.Length%> results sorted by&nbsp;</span><select id="SortBySelect" name="sortBy"><option value="Rank">Rank</option><option value="Name">Name</option><option value="Path">Path</option><option value="ModifiedDate">ModifiedDate</option><option>Size</option></select>&nbsp;<span  style="color:Black"> in &nbsp;<select id="SortByReverse"><option value="0">Ascending</option><option value="1">Descending</option></select>&nbsp;order</span>
</div>
<br /></br />
<div class="paginated">
<%
    Array.Sort(ViewData.Model.ResultsArray);
    foreach (SearchUI.Models.SearchResult results in ViewData.Model.ResultsArray)
    {
        if (results != null)
        {
            %>

    <div class="record">
    <div><a target="_blank" href="<%= results.HREF.ToString() %>"><%= results.Name.ToString()%></a>&nbsp;&nbsp;<img src="Content/file<%= results.Ext.ToString() %>.png"  style="vertical-align:middle;" />&nbsp;&nbsp;<span style="font-size:x-small">Modified:&nbsp;<%= results.ModTime%></span>&nbsp;&nbsp;<br /><span style="color:black;font-size:x-small">Rank:&nbsp;<%= results.Rank%></span></div><span style="color: green" id="pathSpan<%= results.Index %>"><%= results.DisplayPath.ToString()%></span> - <%= results.SizeInBytes%> bytes <br /><br />

    </div>
  <% }
    } 
           %>
  
  </div>
  <% 
    }
   }
   else
   {
       %>
    <p>There were errors with the search.</p>
    <p><strong><%= ViewData.Model.SearchErrors%></strong></p>
  
  <% } %>
  
      <script type="text/javascript">
          $().ready(function() {

              $('div.paginated  div.record:odd').addClass('odd');
              $('div.paginated div.record:even').addClass('even');

              $('div.paginated').each(function() {
                  var currentPage = 0;
                  var numPerPage = 7;
                  var $table = $(this);
                  var repaginate = function() {
                      $table.find('div.record').show().slice(0, currentPage * numPerPage).hide().end()
                         .slice(((currentPage + 1) * numPerPage - 1) + 1).hide().end();
                  };

                  var numRows = $table.find('div.record').length;
                  var numPages = Math.ceil(numRows / numPerPage);
                  var $pager = $('<div class="pager"></div>');
                  $('<span class="page-first">' + '&lt;' + '</span>').bind('click', { 'newPage': page }, function(event) {
                      if (currentPage > 0) {
                          currentPage = currentPage - 1;
                          $('.page' + currentPage).addClass('active').siblings().removeClass('active');
                      }
                      repaginate();
                  }).appendTo($pager).addClass('clickable');



                  for (var page = 0; page < numPages; page++) {
                      if (page % 20 == 0 && page != 0) {
                          $('<br /><br />').appendTo($pager);
                      }
                      $('<span class="page-number page' + page + '">' + (page + 1) + '</span>').bind('click', { 'newPage': page }, function(event) {
                          currentPage = event.data['newPage'];
                          repaginate();
                          $('.page' + currentPage).addClass('active').siblings().removeClass('active');
                      }).appendTo($pager).addClass('clickable');
                  }
                  $('<span class="page-last">' + '&gt;' + '</span>').bind('click', { 'newPage': page }, function(event) {
                      if (currentPage < numPages - 1) {
                          currentPage = currentPage + 1;
                          $('.page' + currentPage).addClass('active').siblings().removeClass('active');
                      }
                      repaginate();
                  }).appendTo($pager).addClass('clickable');
                  $pager.find('span.page-number:first').addClass('active');
                  $pager.clone(true).insertAfter($table);
                  $pager.insertBefore($table);
                  repaginate();
              });

              $('#SortBySelect').change(function() {
                  var sortBy = $('#SortBySelect :selected').val();
                  var rev = $('#SortByReverse :selected').val();
                  var date = new Date();
                  $('#search_results').html('<div style="display:block;text-align: center; margin: 50px 0 0 280px ; width:18em;"><img src="Content/spinner-big.gif" /><br/><br/><span class="loading">Loading...</span><br/><br/><br/></div>');
                  $.ajax({
                      url: 'Search/RunSearch',
                      data: 'search_terms=' + $('#search_terms').val() + '&time=' + date.getTime() + '&search_id=smw_docs&sortBy=' + sortBy + '&reverseSort=' + rev + '&format=' + $('#FormatMenu :selected').val(),
                      type: 'POST',
                      dataType: 'html',
                      timeout: 20000,
                      error: function() {
                          alert('Error running the search');
                      },
                      success: function(TheHtml) {
                          $('#search_results').html(TheHtml).show('1');
                      }
                  });
              });
              $('#SortByReverse').change(function() {
                  var sortBy = $('#SortBySelect :selected').val();
                  var rev = $('#SortByReverse :selected').val();
                  var date = new Date();
                  $('#search_results').html('<div style="display:block;text-align: center; margin: 50px 0 0 280px ; width:18em;"><img src="Content/spinner-big.gif" /><br/><br/><span class="loading">Loading...</span><br/><br/><br/></div>');
                  $.ajax({
                      url: 'Search/RunSearch',
                      data: 'search_terms=' + $('#search_terms').val() + '&time=' + date.getTime() + '&search_id=smw_docs&sortBy=' + sortBy + '&reverseSort=' + rev + '&format=' + $('#FormatMenu :selected').val(),
                      type: 'POST',
                      dataType: 'html',
                      timeout: 20000,
                      error: function() {
                          alert('Error running the search');
                      },
                      success: function(TheHtml) {
                          $('#search_results').html(TheHtml).show('1');
                      }
                  });
              });
              $('#SortBySelect').val("<%= ViewData.Model.SortedBy %>");
              $('#SortByReverse').val("<%= ViewData.Model.ReverseSort %>");
          }); 
    </script>